﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization; 
using System.Linq;
using System.Text;
using System.Threading; 
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (Session["SELECTED_LANGUAGE"] != null)
        {
            string current_language = Session["SELECTED_LANGUAGE"].ToString();

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(current_language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(current_language);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
        }

        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e) 
    {
        Label2.Text = "Logged in as: " + Context.User.Identity.Name;

        StringBuilder sb = new StringBuilder(); 

        dbProcessor my_db = new dbProcessor();

        cmpUser current_user = new cmpUser();

        int current_user_id = current_user.get_id(Context.User.Identity.Name);

        cmpEntry[] user_comps = my_db.select_user_entries(current_user_id);
        
        TableRow Table1Header = new TableRow();

        // Абсолютно костыльная, немасштабируемая архитектура 
        // сделал, чтобы посмотреть как будет выглядеть, завести баг на рефакторинг 
        
        TableCell tc1 = new TableCell();

        tc1.BorderWidth = 1;
        tc1.Font.Bold = true; 

        tc1.Text = GetLocalResourceObject("FieldName_ID").ToString();
        
        TableCell tc2 = new TableCell();

        tc2.BorderWidth = 1;
        tc2.Font.Bold = true; 

        tc2.Text = GetLocalResourceObject("FieldName_Name").ToString();

        TableCell tc3 = new TableCell();
        
        tc3.BorderWidth = 1;
        tc3.Font.Bold = true; 

        tc3.Text = GetLocalResourceObject("FieldName_Address").ToString();

        TableCell tc4 = new TableCell();
        
        tc4.BorderWidth = 1;
        tc4.Font.Bold = true; 

        tc4.Text = GetLocalResourceObject("FieldName_Email").ToString();

        TableCell tc5 = new TableCell();
        
        tc5.BorderWidth = 1;
        tc5.Font.Bold = true; 

        tc5.Text = GetLocalResourceObject("FieldName_Logotype").ToString();

        Table1Header.Cells.AddAt(0,tc1);
        Table1Header.Cells.AddAt(1,tc2);
        Table1Header.Cells.AddAt(2,tc3);
        Table1Header.Cells.AddAt(3,tc4);
        Table1Header.Cells.AddAt(4,tc5);

        Table1.Rows.Add(Table1Header); 
       
        foreach (cmpEntry cmp in user_comps)
        {
            TableRow tr = new TableRow();
            
            TableCell ID = new TableCell();
            TableCell Name = new TableCell();
            TableCell Address = new TableCell();
            TableCell Email = new TableCell();
            TableCell Logotype = new TableCell();
            TableCell EditLink = new TableCell();
            TableCell DeleteLink = new TableCell();

            // ---- Костыли!!! Убрать! 

            ID.BorderWidth = 1;
            Name.BorderWidth = 1;
            Address.BorderWidth = 1;
            Email.BorderWidth = 1;
            Logotype.BorderWidth = 1;
            EditLink.BorderWidth = 1;
            DeleteLink.BorderWidth = 1; 

            //-------------------------

            ID.Text = "<A href=viewDetails.aspx?CompId=" + cmp.cmpID + ">" + cmp.cmpID + "</A>";
            Name.Text = cmp.cmpName;
            Address.Text = cmp.cmpAddress;
            Email.Text = cmp.cmpEmail;
            Logotype.Text = "<IMG SRC=imageViewSmall.ashx?CompId=" + cmp.cmpID + ">";
            EditLink.Text = "<A href=updateEntry.aspx?CompId=" + cmp.cmpID + ">Edit</A>";
            DeleteLink.Text = "<A href=deleteEntry.aspx?CompId=" + cmp.cmpID + ">Delete</A>"; 

            tr.Cells.Add(ID);
            tr.Cells.Add(Name);
            tr.Cells.Add(Address);
            tr.Cells.Add(Email);
            tr.Cells.Add(Logotype);
            tr.Cells.Add(EditLink);
            tr.Cells.Add(DeleteLink);

            Table1.Rows.Add(tr);
        }
        
        logoutButton.Visible = true; 
        
        // Session.Add("UserID", current_user_id);
     
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("postNew.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("welcome.aspx"); 
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");
    }
    protected void Link_EN_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "en-US");
        Response.Redirect(Request.Url.AbsoluteUri); 
    }
    protected void Link_RU_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "ru-RU");
        Response.Redirect(Request.Url.AbsoluteUri); 
    }
}